/**
 * 根目录下执行{node buildArea.js}
 * 构建省市区，需把areaCodes中的{export default areaCodes}改为{module.exports = areaCodes}
 */

const fs = require('fs');
const areaCodes = require('./src/utils/areaCodes');
const provinces = [];
const citys = [];
const areas = [];

for (let areaCode of areaCodes) {
    if (areaCode.code.includes("0000")) {
        provinces.push(areaCode);
    } else if (areaCode.code.includes("00")) {
        citys.push(areaCode);
    } else {
        areas.push(areaCode);
    }
}

for (let province of provinces) {
    province.parentCode = '000000';
}

for (let city of citys) {
    city.parentCode = city.code.substr(0, 2) + "0000";
}

for (let area of areas) {
    area.parentCode = area.code.substr(0, 4) + "00";
}

const provinceStr = "const provinceList = " + JSON.stringify(provinces) + ";";
const cityStr = "const cityList = " + JSON.stringify(citys) + ";";
const areaStr = "const areaList = " + JSON.stringify(areas) + ";";

fs.writeFile('./areaData.js', provinceStr + cityStr + areaStr, 'utf8', (e) => {
    if (e) throw e;
});
